package com.User;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.Result.BaseRespones;
import com.Result.StatusCode;
import com.Washer.Dormitory;
import com.Washer.DormitoryMapper;
import com.Washer.Washer;
import com.Washer.WasherMapper;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class AdminOperation {
	
	public static Admin getAdminbyID(int adminID) throws IOException {
		String resource = "mybatisConfig.xml";
		InputStream is = org.apache.ibatis.io.Resources.getResourceAsStream(resource);
		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
		SqlSession session = sessionFactory.openSession();
		AdminMapper mapper=(AdminMapper)session.getMapper(AdminMapper.class);
		Admin admin;

		Map paramMap=new HashMap();
	    paramMap.put("AdminUserID",adminID);
	    admin=mapper.getAdmin(paramMap).get(0);
	    if (admin==null) {
			return null;
		}
	    return admin;
	}
	
	public int insertAdmin(Admin admin) throws IOException {
		String resource = "mybatisConfig.xml";
		InputStream is = org.apache.ibatis.io.Resources.getResourceAsStream(resource);
		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
		SqlSession session = sessionFactory.openSession();
		AdminMapper mapper=(AdminMapper)session.getMapper(AdminMapper.class);
		
		System.out.print(admin);
		int rs=mapper.insertAdmin(admin);
		if (rs>0){
			session.commit();
			session.close();
		}
		else {
			return -1;
		}
		return admin.getAdminID();	
	}

	
	
}
